﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;

namespace IntelligentCMS
{
    public partial class CMSPageConfig : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (id == -1)
            {
                //Button_AddField.Visible = false;

            }
            else
            {

                RefreshGrid();

                //Execute();
                //Button_AddField.Visible = true;
                //DropDownList_Fields.Visible = true;
            }
        }

        public long ID {
            set
            {
                this.id = id;
            }

        }

        private long id = -1;

        public void Execute()
        {


        }

        protected void Button_Save_Click(object sender, EventArgs e)
        {

            string sqlquery = string.Empty;

            if (id > -1)
            {


                sqlquery = "UPDATE [Page] SET [Name] = '" + TextBox_Name.Text + "', [Description] = '" + TextBox_Description.Text + "' WHERE [ID] = " + id;    

                System.IO.TextWriter newWriter = new System.IO.StreamWriter("");
               // newWriter.Write(theEditor.Content);

                newWriter.Close();    
                 


            }
            else
            {
                

                CMSDbDataLayer insertcontent = new CMSDbDataLayer(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString, CMSDbDataHelper.Microsoft);
                insertcontent.AddQuery("INSERT INTO [Page] ([FileID], [Name], [Description], [DateTime]) VALUES ('" + TextBox_Name.Text + "', '" + TextBox_Name.Text + "', '" + TextBox_Description.Text + "', '" + DateTime.Now + "')");
                insertcontent.SelectQuery = false;
                insertcontent.Execute();

                RefreshGrid();
            }

            
        }

        private void RefreshGrid()
        {




            CMSDbDataLayer fields = new CMSDbDataLayer(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString, CMSDbDataHelper.Microsoft);
            fields.AddQuery("SELECT [Name], [Description], [ID] FROM Page WHERE ID = " + id);
            fields.SelectQuery = true;
            fields.Execute();

            List<CMSColumnType> newCMSColumnTypes = new List<CMSColumnType>();
            CMSColumnType CMSColumnType_Name = new CMSColumnType(CMSColumnType.CMSCOLUMNTYPE_Text, string.Empty, string.Empty);
            CMSColumnType CMSColumnType_Description = new CMSColumnType(CMSColumnType.CMSCOLUMNTYPE_Text, string.Empty, string.Empty);

            newCMSColumnTypes.Add(CMSColumnType_Name);
            newCMSColumnTypes.Add(CMSColumnType_Description);


            for (int i = 0; i < fields.Data.Rows.Count; i++)
            {
                //fields.Data.Rows[i]["ID"] = newDataLayer.Data.Rows[i]["ID"].ToString();
                fields.Data.Rows[i]["Name"] = common.FromBase64String(fields.Data.Rows[i]["Name"].ToString());
                fields.Data.Rows[i]["Description"] = common.FromBase64String(fields.Data.Rows[i]["Description"].ToString());
                //fields.Data.Rows[i]["Edit"] = newDataLayer.Data.Rows[i]["ID"].ToString();

            }


            //newDataLayer.Data.Columns["Link"].ColumnName = string.Empty;


            theCMSDataGrid.Data = fields.Data;
            theCMSDataGrid.CMSColumnTypes = newCMSColumnTypes;
            theCMSDataGrid.Execute();
            //theCMSDataGrid.Execute();

        }
    }
}